<?php
namespace app\controller;

use app\Controller;
use app\model\Sms as Model;
use app\model\User;
use app\helper\Ident;

class Sms extends Controller
{
    /**
     * 发送验证码.
     * 
     * @return array
     */
    public function save()
    {
        $model = new Model;
        $model->save($this->request->post());
        return json($model->toArray());
    }
    /**
     * 短信登录.
     * 
     * @return array
     */
    public function login()
    {
        // 获取token
        $model = User::where('mobile', input('post.mobile/s'))->findOrEmpty();
        if($model->isEmpty()){
            return json(['code' => 0, 'msg' => '当前用户不存在']);
        }
        $data = ['access_token' => Ident::token($model->id, input('post.mobile/s')), 'id' => $model->id]; //参数为用户认证的信息，请自行添加
        redis()->hset('AccessTokenList', $data['access_token'], $data + ['login_at' => time()]);
        return json($data);
    }
    /**
     * 重置密码.
     * 
     * @return array
     */
    public function reset()
    {
        // 获取token
        $model = User::where('mobile', input('post.mobile/s'))->findOrFail();
        $model->password = md5(input('post.password/s'));
        $model->save();
        $data = ['access_token' => Ident::token(['uid' => $model->id, 'mobile' => $model->mobile]), 'id' => $model->id]; //参数为用户认证的信息，请自行添加
        redis()->hset('UserAccessTokenList', $data['access_token'], $data + ['login_at' => time()]);
        return json($data);
    }
}
